<template>
  <div class="pane">
    <div
      v-if="loggedIn"
      class="order-list"
    >
      <div
        v-for="item in pendingOrder.records"
        :key="item.id"
        class="order-card"
      >
        <div class="order-card-item left">
          <div
            class="item-type"
            :class="{buy: item.side === 2}"
          >{{ item.side === 1 ? $t('卖') : $t('买') }}</div>
        </div>
        <div class="order-card-item right">
          <div class="item-row">
            <div class="row-symbol">{{ `${coinPair.stock}/${coinPair.money}` }}</div>
            <div class="row-time">{{ item.ctime | timeFormat() }}</div>
          </div>
          <div class="item-row">
            <div class="row-amount">{{$t('数量')}}：{{ item.amount }}</div>
          </div>
          <div class="item-row">
            <div class="row-price">{{$t('价格')}}：{{ item.price | numberFormat() }}</div>
          </div>
          <div class="item-row item-row-last">
            <div class="row-total">
              <span class="total-label">{{$t('总价')}}</span>
              <span class="total-num">{{ item.amount * item.price | numberFormat() }}{{ coinPair.money }}</span>
            </div>
            <div
              class="btn-cancel"
              @click="handleCancelOrder(item.id)"
            >{{ $t('撤销') }}</div>
          </div>
        </div>
      </div>
    </div>
    <div
      v-else
      class="jilu"
    >
      <i class="iconfont icon-chazhaojilu"></i>
      <div style="clear: both;"></div>
      <nuxt-link to="/login">{{$t('去登陆')}}</nuxt-link>
    </div>
  </div>
</template>
<script>
import { mapState, mapGetters, mapActions } from 'vuex'
export default {
  computed: {
    ...mapState({
      loggedIn: ({ auth }) => auth.loggedIn,
      symbol: ({ exchange }) => exchange.symbol,
      serverAuthed: ({ exchange }) => exchange.serverAuthed,
      pendingOrder: ({ exchange }) => exchange.pendingOrder
    }),
    ...mapGetters({
      coinPair: 'market/coinPair'
    })
  },
  watch: {
    serverAuthed (val, oldVal) {
      if (val) {
        this.queryPendingOrder({ symbol: this.symbol })
      }
    },
    symbol (val) {
      if (val) {
        this.queryPendingOrder({ symbol: val })
      }
    }
  },
  beforeMount () {
    if (this.serverAuthed) {
      this.queryPendingOrder({ symbol: this.symbol })
    }
  },
  methods: {
    ...mapActions({
      cancelOrder: 'exchange/cancelOrder',
      queryPendingOrder: 'exchange/queryPendingOrder'
    }),
    handleCancelOrder (orderId) {
      this.$toast.show(this.$t('确定撤销吗？'), {
        action: {
          text: this.$t('撤销'),
          onClick: () => {
            const succeed = ({ message }) => {
              this.queryPendingOrder({ symbol: this.symbol })
              this.$toast.success(message)
            }
            const failed = (error) => {
              this.$toast.error(error.message)
            }
            const payload = {
              market: this.symbol,
              order_id: orderId
            }
            this.cancelOrder(payload)
              .then(succeed)
              .catch(failed)
          }
        }
      })
    }
  }
}
</script>
<style scoped>
.order-list {
  padding: 0 2px;
  overflow-y: auto;
}
.order-card {
  display: flex;
  padding: 10px 8px;
  border-bottom: 1px solid #f3f3f3;
}
.item-type {
  width: 50px;
  height: 50px;
  border-radius: 50px;
  line-height: 50px;
  text-align: center;
  background-color: #d14b64;
  color: white;
  font-size: 16px;
}
.item-type.buy {
  background-color: #03ac8f;
}
.order-card-item.left {
  display: flex;
  align-items: center;
}
.order-card-item.right {
  flex: 1;
  margin-left: 8px;
}
.item-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.item-row-last {
  margin-top: 10px;
}
.row-symbol {
  font-size: 16px;
}
.row-time {
  font-size: 12px;
  color: #999;
}
.row-amount {
  font-size: 12px;
  color: #999;
}
.row-price {
  font-size: 12px;
  color: #999;
}

.total-label {
  font-size: 12px;
  color: #999;
}
.total-num {
  font-size: 16px;
}

.btn-cancel {
  display: inline-block;
  border: 1px solid #2285e2;
  color: #2285e2;
  width: 60px;
  height: 26px;
  line-height: 26px;
  border-radius: 26px;
  outline: none;
  text-align: center;
}
</style>
